import {
  getMessagePageList,
  delMessageDetail,
  batchDelMessagesDetail
} from "@/api/message";
import { ref } from "vue";
import { useMessageStoreHook } from "@/store/modules/message";

// 消息存储
const messageStore = useMessageStoreHook();

// 空数据提示（错误提示）
export const tableEmptyText = ref("");

const request = {
  get: {
    // 获取列表（分页）
    list: async (params: object) => {
      try {
        const res = await getMessagePageList(params);
        tableEmptyText.value = "资料暂无";
        return {
          data: res.data.list,
          total: res.data.total
        };
      } catch (error) {
        tableEmptyText.value = error.message;
      }
    }
  },
  put: {
    // 标记已读/未读
    read: async (id: Id, read: boolean) => {
      await messageStore.setMsgReadStatus(id, read);
    },
    // 全部已读
    readAll: async () => {
      await messageStore.setMsgReadAll();
    }
  },
  delete: {
    // 删除（单个）
    remove: async id => {
      await delMessageDetail(id);
    },
    // 删除（批量）
    batch: async ids => {
      await batchDelMessagesDetail({
        ids
      });
    }
  }
};

export default request;
